﻿using System.ComponentModel.Composition;
using System.IO;
using DeepZoomPivotConstructor.VisualLib;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RenderEngine;
using RenderEngineInterfaces;
using SimpleFileRendering;

namespace t_SimpleFileRendering
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class t_StringToFileRender
    {
        public t_StringToFileRender()
        {
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        [TestInitialize]
        public void InitForTest()
        {
            t_TestingLibrary.Utilities.MyClassInit();
            t_TestingLibrary.Utilities.AddPart(new RenderControl());
            t_TestingLibrary.Utilities.AddPart(new LayoutHelper());
        }

        [TestCleanup]
        public void ShutDownTest()
        {
            t_TestingLibrary.Utilities.MyClassDone();
        }

        [TestMethod]
        public void TestNonExistantFilePriority()
        {
            StringToFileInfoRender sf = new StringToFileInfoRender();
            t_TestingLibrary.Utilities.Compose(sf);
            Assert.AreEqual(RenderPluginPriority.RPPCantDoIt, sf.RenderPriority("f:\\bogus\\dude.jpeg"));
        }

        [TestMethod]
        [DeploymentItem("wallpaper.jpg")]
        public void TestFileThatExistsPriority()
        {
            StringToFileInfoRender sf = new StringToFileInfoRender();
            PrepareObject(sf);
            Assert.AreEqual(RenderPluginPriority.RPPGeneric, sf.RenderPriority(@"wallpaper.jpg"));
        }


        /// <summary>
        /// To get out the individual variables.
        /// </summary>
        class testme
        {
            [Export("RelativePathBase")]
            public DirectoryInfo localLocation;
        }

        /// <summary>
        /// Setup for MEF the render
        /// </summary>
        /// <param name="sf"></param>
        private void PrepareObject(object sf)
        {
            var dude = new testme() { localLocation = new DirectoryInfo(".") };
            t_TestingLibrary.Utilities.AddPart(dude);
            t_TestingLibrary.Utilities.Compose(sf);
        }

        [TestMethod]
        [DeploymentItem("wallpaper.jpg")]
        public void TestFileThatExistsButNoLocal()
        {
            StringToFileInfoRender sf = new StringToFileInfoRender();
            t_TestingLibrary.Utilities.Compose(sf);
            Assert.AreEqual(RenderPluginPriority.RPPCantDoIt, sf.RenderPriority(@"wallpaper.jpg"));
            Assert.AreEqual(RenderPluginPriority.RPPGeneric, sf.RenderPriority(string.Format(@"{0}\wallpaper.jpg", System.Environment.CurrentDirectory)));
        }

        [TestMethod]
        public void TestPriorityNoFile()
        {
            StringToFileInfoRender sf = new StringToFileInfoRender();
            t_TestingLibrary.Utilities.Compose(sf);
            Assert.AreEqual(RenderPluginPriority.RPPCantDoIt, sf.RenderPriority("TestPriorityNoFile.jpg"));
        }

        [TestMethod]
        [DeploymentItem("wallpaper.jpg")]
        public void TestFileThatExistsRendering()
        {
            StringToFileInfoRender sf = new StringToFileInfoRender();
            t_TestingLibrary.Utilities.AddPart(new JPEGFileRender());
            PrepareObject(sf);
            var vfuture = sf.Render(@"wallpaper.jpg");
            Assert.IsNotNull(vfuture, "Error - the future came back null!");
            Visual v = vfuture.Result;
            Assert.IsNotNull(v, "The visual should have come back from the rendering");
            Assert.AreEqual(1, v.SGNodeList.Count, "Wrong number of actual images");
        }

        [Import]
        public IRenderEngine MyRender { get; set; }

        [TestMethod]
        [DeploymentItem("wallpaper.jpg")]
        public void TestMetaData()
        {
            t_TestingLibrary.Utilities.AddPart(new StringToFileInfoRender());
            t_TestingLibrary.Utilities.AddPart(new JPEGFileRender());
            PrepareObject(this);
            Visual v = MyRender.Render(@"wallpaper.jpg");
            Assert.IsNotNull(v, "Visual rendering of file should not have been null!");
            Assert.AreEqual(1, v.SGNodeList.Count, "Expected just one image to come back!");
            Assert.IsNotNull(v.Info, "Expecting basic info about the image");
            Assert.AreEqual("wallpaper", v.Info.Title, "Title on image visual object is not right");
        }
    }
}
